package com.unicom.projt.query;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.unicom.projt.Home;
import com.unicom.projt.InputPhoneNumberPage;
import com.unicom.projt.R;
import com.unicom.projt.util.*;

import java.util.*;

/**
 * Created by IntelliJ IDEA.
 * User: Lee
 * Date: 11-12-7
 * Time: 上午9:33
 */
public class QueryPaymentPage extends Activity implements View.OnClickListener {
    private SimpleAdapter listAdapter;
    private ListView mListView;
    private List<HashMap<String, String>> listData = new ArrayList<HashMap<String, String>>();

    private List<String> selectData = new ArrayList<String>();
    private Spinner spinner;
    private ArrayAdapter<String> adapter;
    private Calendar calendar = Calendar.getInstance();
    private int[] colors = new int[]{Color.WHITE, Color.LTGRAY};

    private Handler mHandler;

    private ProgressDialog mDialog;
    private static final int DIALOG_PROGRESS = 11;
    private QueryController mController;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.query_payment);
        mHandler=new Handler();
        mController=new QueryController(this);
        initData();
        this.findViewById(R.id.query_payment_back_btn).setOnClickListener(this);
        this.findViewById(R.id.query_payment_logout_btn).setOnClickListener(this);
    }

    private void initData() {
        mListView = (ListView) this.findViewById(R.id.query_payment_list_view);


        listAdapter = new SimpleAdapter(this, listData, R.layout.query_payment_list_item,
                new String[]{"phoneType", "phoneNumber", "paymentDate", "amount"},
                new int[]{R.id.item_phone_type, R.id.item_phone_number, R.id.item_pay_date, R.id.item_pay_amount}){
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View view = super.getView(position, convertView, parent);
                int colorPos = position % colors.length;
                view.setBackgroundColor(colors[colorPos]);
                return view;
            }
        };
        mListView.setAdapter(listAdapter);


        initSpinnerData();
        spinner = (Spinner) findViewById(R.id.query_payment_dropdown);
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, selectData);
        adapter.setDropDownViewResource(android.R.layout.select_dialog_item);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(new SpinnerSelectedListener());
    }

    private void queryPaymentData(Date date) {
        listData.clear();
        listAdapter.notifyDataSetChanged();
        showDialog(DIALOG_PROGRESS);
        mController.queryPM(UserHolder.phoneNumber,date);
    }
    public void updatePaymentInfo(List<String> records) {
        if(records.size()==0){
            AlertDialogUtils.showConfirmDiaLog(this,"无缴费记录");
        }
        
        listData.clear();
        for(String record : records){
            HashMap<String, String> map = new HashMap<String, String>();
            String phoneNumber=SimpleXmlParser.getElementsValue(record,"TelNo").get(0);
            String paymentDate=SimpleXmlParser.getElementsValue(record,"ProcessTime").get(0);
            String amount=SimpleXmlParser.getElementsValue(record,"Amout").get(0);
            double li=Double.parseDouble(amount);
            String numberType=UserHolder.phoneNumber.equals(phoneNumber)?"本机":"其他号码";
            map.put("phoneType", numberType);
            map.put("phoneNumber", phoneNumber);
            map.put("paymentDate", paymentDate);
            map.put("amount", "+"+li/100 + "元");
            listData.add(map);
        }
//        map.put("phoneType", "本机");
//        map.put("phoneNumber", "18612345678");
//        map.put("paymentDate", DateUtils.formatDate(new Date()));
//        map.put("amount", "+200元");
//        listData.add(map);
//
//        map = new HashMap<String, String>();
//        map.put("phoneType", "其他号码");
//        map.put("phoneNumber", "18622345678");
//        map.put("paymentDate", DateUtils.formatDate(new Date()));
//        map.put("amount", "+100元");
//        listData.add(map);
//        map = new HashMap<String, String>();
//        map.put("phoneType", "其他号码");
//        map.put("phoneNumber", "18622345678");
//        map.put("paymentDate", DateUtils.formatDate(new Date()));
//        map.put("amount", "+100元");
//        listData.add(map);
//        map = new HashMap<String, String>();
//        map.put("phoneType", "其他号码");
//        map.put("phoneNumber", "18622345678");
//        map.put("paymentDate", DateUtils.formatDate(new Date()));
//        map.put("amount", "+100元");
//        listData.add(map);
//        map = new HashMap<String, String>();
//        map.put("phoneType", "其他号码");
//        map.put("phoneNumber", "18622345678");
//        map.put("paymentDate", DateUtils.formatDate(new Date()));
//        map.put("amount", "+100元");
//        listData.add(map);
//        map = new HashMap<String, String>();
//        map.put("phoneType", "其他号码");
//        map.put("phoneNumber", "18622345678");
//        map.put("paymentDate", DateUtils.formatDate(new Date()));
//        map.put("amount", "+100元");
//        listData.add(map);
//        map = new HashMap<String, String>();
//        map.put("phoneType", "其他号码");
//        map.put("phoneNumber", "18622345678");
//        map.put("paymentDate", DateUtils.formatDate(new Date()));
//        map.put("amount", "+100元");
//        listData.add(map);
//        map = new HashMap<String, String>();
//        map.put("phoneType", "其他号码");
//        map.put("phoneNumber", "18622345678");
//        map.put("paymentDate", DateUtils.formatDate(new Date()));
//        map.put("amount", "+100元");
//        listData.add(map);
//        map = new HashMap<String, String>();
//        map.put("phoneType", "其他号码");
//        map.put("phoneNumber", "18622345678");
//        map.put("paymentDate", DateUtils.formatDate(new Date()));
//        map.put("amount", "+100元");
//        listData.add(map);
//        map = new HashMap<String, String>();
//        map.put("phoneType", "其他号码");
//        map.put("phoneNumber", "18622345678");
//        map.put("paymentDate", DateUtils.formatDate(new Date()));
//        map.put("amount", "+100元");
//        listData.add(map);
//        map = new HashMap<String, String>();
//        map.put("phoneType", "其他号码");
//        map.put("phoneNumber", "18622345678");
//        map.put("paymentDate", DateUtils.formatDate(new Date()));
//        map.put("amount", "+100元");
//        listData.add(map);

        listAdapter.notifyDataSetChanged();
    }

    private void initSpinnerData() {
        calendar.setTime(new Date());
        for (int i = 1; i < 7; i++) {
            String data = DateUtils.formatDateToYearAndMonth(calendar.getTime());
            selectData.add(data);
            calendar.add(Calendar.MONTH, -1);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.query_payment_back_btn:
                PageJumpUtils.jump(this, Home.class);
                this.finish();
                break;
            case R.id.query_payment_logout_btn:
                AlertDialogUtils.showYesNoDiaLog(this, "是否注销？", new AlertDialogUtils.Executor() {
                    @Override
                    public void execute() {
                        UserHolder.clean();
                        doLogout();
                    }
                });
                break;
        }
    }
    private void doLogout(){
        Intent intent = new Intent(this, InputPhoneNumberPage.class);
        this.startActivity(intent);
        this.finish();
    }
    @Override
    public void onBackPressed() {
        PageJumpUtils.jump(this, Home.class);
        this.finish();
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_SEARCH) {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case DIALOG_PROGRESS:
                mDialog = new ProgressDialog(this);
                mDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                mDialog.setMessage("正在处理，请等待......");
                mDialog.setCancelable(false);
                mDialog.setOnKeyListener(new DialogInterface.OnKeyListener(){
                    public boolean onKey(DialogInterface dialog, int keyCode,KeyEvent event) {
                        if (keyCode == KeyEvent.KEYCODE_SEARCH) {
                            return true;
                        }
                        return false;
                    }
                });
                return mDialog;
            default:
                return null;
        }
    }

    public void hideProgressDialog() {
        if (mDialog != null) {
            removeDialog(DIALOG_PROGRESS);
        }
    }

    class SpinnerSelectedListener implements AdapterView.OnItemSelectedListener {
        private Handler mHandler = new Handler();

        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
                                   long arg3) {
            calendar.setTime(new Date());
            calendar.add(Calendar.MONTH, 0- arg2);
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    queryPaymentData(calendar.getTime());
                }
            });
        }

        public void onNothingSelected(AdapterView<?> arg0) {
        }
    }
}
